package cn.hxz.controller;

import cn.hxz.bean.AjaxResult;
import cn.hxz.utils.file.FileUploadUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: TODO
 * @Author: huangxinzhang
 * @Date: 2024/9/12
 **/
@Controller
@RequestMapping("/common")
public class CommonController {

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/uploadImg")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile singleFile, HttpServletRequest request) throws Exception
    {
        System.out.println("uploadFile.....");
        System.out.println(singleFile);

        String profile = "D:/ruoyi/uploadPath" + "/upload";
        try
        {
            // 上传文件路径
            //String filePath = profile;
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.uploadFile(singleFile, request);

            AjaxResult ajax = AjaxResult.success();
//            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUploadUtils.getName(fileName));
            ajax.put("originalFilename", singleFile.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

}
